package seabattle.io;

import seabattle.field.BattleField;
import seabattle.field.Cell;

public class WebReflector {

	public String fieldsToHTML(BattleField field) {
		final String rowLetter = "abcdefghij";
		final String columnLine = "&nbsp&nbsp0&nbsp&nbsp1&nbsp&nbsp2&nbsp&nbsp3&nbsp&nbsp4&nbsp"
				+ "&nbsp5&nbsp&nbsp6&nbsp&nbsp7&nbsp&nbsp8&nbsp&nbsp9<br>";
		StringBuilder out = new StringBuilder(columnLine);
		for (int row = 0; row < BattleField.SIZE; row++) {
			char rowName = rowLetter.charAt(row);
			out.append(rowName);
			for (Cell column : field.getColumn(row)) {
				out.append(createCell(column, field.getVisible()));
			}
			out.append(rowName).append("<br>");
		}
		out.append(columnLine);
		return out.toString();
	}

	private String createCell(Cell cell, boolean showShips) {
		switch (cell) {
		case EMPTY:
			return "&nbsp.&nbsp";
		case SHIP:
			if (showShips) {
				return "[&nbsp]";
			} else {
				return "&nbsp.&nbsp";
			}
		case SHOT:
			return "&nbsp*&nbsp";
		case HIT:
			return "[*]";
		default:
			return "!";
		}
	}

}
